/*
 * 
 * ZigZag Conversion
 * 
 * 
 * The string "PAYPALISHIRING" is written in a zigzag pattern on a given number 
 * of rows like this: (you may want to display this pattern in a fixed font for better legibility)
 * 
 * 
 */
#include <string>
#include <iostream>
using namespace std;

string convert(string s, int nRows) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function    
    
    if(nRows == 1)
        return s;
    
    int length = s.size();
     string result="";
    if(nRows == 2)
    {
        for(int i=0;i<length;i=i+2)
        {
            result+=s[i];
        }
        for(int i=1;i<length;i=i+2)
        {
             result+=s[i];
        }
       
    }
    
    if(nRows >= 3)
    {
        for(int row=0;row<nRows;row++)
        {
            int iRow = row;
       
            if(row==nRows-1)
            {
                iRow=0;
            }
 
            for(int i=row;i<length;i=i+(2*nRows-(2*(iRow+1))))
            {
                 if(row==0||row==nRows-1)
                 {
                      result+=s[i];
                 }
                 else
                 {
                     if(i==row)
                     {
                     result+=s[i];
                     }
                     else
                     {
                         result+=s[i];
                         i=i+2*row;
                         if(i<length)
                         {
                             result+=s[i];
                         }
                     }
                 }
    
            }
        }
    }
    
         return result;
    }

void TestZigZagConvert()
{
    //PAHNAPLSIIGYIR
    string s = "PAYPALISHIRING";
    convert(s,3);
}